namespace CloudNinja.Metering.Common
{
    using CloudNinja.Metering.Common.Logging;

    using Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling;

    // TODO: Revisit
    public class MeteringContext : IMeteringContext
    {

        private readonly ILogger logger;

        public MeteringContext(ILogger logger, RetryPolicy sqlCommandRetryPolicy, RetryPolicy sqlRetryPolicy, RetryPolicy azureStorageRetryPolicy)
        {
            Helper.Guard(logger, "logger");

            this.logger = logger;

            this.SqlAzureCommandRetryPolicy = sqlCommandRetryPolicy;
            this.SqlAzureCommandRetryPolicy.Retrying += (sender, args) =>
            {
                var message =
                    string.Format(
                        "Transitional retry for SQL Command - Delay:{0}, Exception:{1}",
                        args.Delay,
                        args.LastException);

                this.logger.LogInformation(message);
            };

            this.SqlAzureConnectionRetryPolicy = sqlRetryPolicy;
            this.SqlAzureConnectionRetryPolicy.Retrying += (sender, args) =>
            {
                var message =
                    string.Format(
                        "Transitional retry for SQL Connection - Delay:{0}, Exception:{1}",
                        args.Delay,
                        args.LastException);

                this.logger.LogInformation(message);
            };

            this.AzureStorageRetryPolicy = azureStorageRetryPolicy;
            this.AzureStorageRetryPolicy.Retrying += (sender, args) =>
            {
                var message =
                    string.Format(
                        "Transitional retry for Azure Storage - Count:{0}, Delay:{1}, Exception:{2}",
                        args.CurrentRetryCount,
                        args.Delay,
                        args.LastException);

                this.logger.LogInformation(message);
            };
        }

        public RetryPolicy AzureStorageRetryPolicy { get; private set; }

        public RetryPolicy SqlAzureConnectionRetryPolicy { get; private set; }

        public RetryPolicy SqlAzureCommandRetryPolicy { get; private set; }
    }
}